#pragma once

#include <thread>
#include <memory>
#include <mutex>
#include <queue>

#include "Task.h"
#include "Selector.h"

namespace nio {

	class EventLoop
	{
	private:
		bool isRunning = false;
		std::shared_ptr<std::thread> thread = nullptr;
		std::priority_queue<std::shared_ptr<Task>, std::vector<std::shared_ptr<Task>>, Task_cmp> tasks;
		std::mutex lock;
	public:
		Selector selector;
	public:
		void startup();
		void shutdown();
		void loop();
		bool inCurrentLoop();
		void assertCurrentLoop();
		void execute(std::function<void()> work);
		std::shared_ptr<Task> execute(int64_t interval, bool repeated, std::function<void()> work);
	};

}